package com.admin.service;

import com.admin.dao.MedicalDao;
import com.admin.entity.Medical;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.admin.sys.utils.admin.StringUtils;
import com.admin.sys.utils.admin.ObjectUtils;
import com.admin.sys.base.module.extend.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;

//就诊信息接口类
@Service
public class MedicalService extends BaseService {

    @Autowired
    private MedicalDao medicalDao;

    //【就诊信息】设置查询条件
    private LambdaQueryWrapper<Medical> getMedicalQueryCondition(Medical medical) {
        LambdaQueryWrapper<Medical> lambdaQuery = this.getBaseQueryCondition(medical);
        //根据创建时间排序
        lambdaQuery.orderByDesc(Medical::getCreateDate);
        if (ObjectUtils.isNotNull(medical)) {
            //【主键id】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(medical.getId()), Medical::getId, medical.getId());
            //【用户账号】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(medical.getRegisterId()), Medical::getRegisterId, medical.getRegisterId());
            //【就诊医生】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(medical.getUserId()), Medical::getUserId, medical.getUserId());
            //【就诊科室】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(medical.getOfficeId()), Medical::getOfficeId, medical.getOfficeId());
            //【疾病名称】模糊查询
            lambdaQuery.like(StringUtils.isNotEmpty(medical.getName()), Medical::getName, medical.getName());
            //【就诊费用】模糊查询
            lambdaQuery.like(StringUtils.isNotEmpty(medical.getFee()), Medical::getFee, medical.getFee());
        }
        return lambdaQuery;
    }

    //【就诊信息】分页查询
    public IPage<Medical> getPage(Page<Medical> page, Medical medical) {
        LambdaQueryWrapper<Medical> lambdaQuery = getMedicalQueryCondition(medical);
        return medicalDao.selectPage(page, lambdaQuery);
    }
    
    //【就诊信息】查询列表
    public List<Medical> getList(Medical medical) {
        LambdaQueryWrapper<Medical> lambdaQuery = getMedicalQueryCondition(medical);
        return medicalDao.selectList(lambdaQuery);
    }
    
    //【就诊信息】根据id查询
    public Medical get(String id) {
        return medicalDao.selectById(id);
    }

    //【就诊信息】根据对象查询
    public Medical get(Medical medical) {
    LambdaQueryWrapper<Medical> lambdaQuery = getMedicalQueryCondition(medical);
        return medicalDao.selectOne(lambdaQuery);
    }
    
    //【就诊信息】新增
    public int insert(Medical medical) {
        this.preInsert(medical);
        return medicalDao.insert(medical);
    }
    
    //【就诊信息】修改
    public int update(Medical medical) {
        this.preUpdate(medical);
        return medicalDao.updateById(medical);
    }
    
    //【就诊信息】删除
    public int delete(String id) {
        return medicalDao.deleteById(id);
    }

    //【就诊信息】批量删除
    public int delAll(String[] ids) {
        return medicalDao.deleteBatchIds(Arrays.asList(ids));
    }

}